<template>
  <div class="flex">
    <!-- 左侧 -->
    <div>
      <div id="webviewDiv" class="w-720px h-500px border-r-1">
      </div>
    </div>
    <!-- 右侧 -->
    <div class="space-y-3">
      <el-button @click="test111">创建webview</el-button>
      <el-button @click="goHomePage">打开主页</el-button>
      <div class="flex p-3">
        <el-input v-model="followHomePageIdValue" placeholder="请输入要关注的用户主页ID" style="width: 400px"></el-input>
        <el-button @click="follow" class="ml-2">关注</el-button>
      </div>
      <div class="flex p-3">
        <el-input v-model="privateMessageHomePageIdValue" placeholder="请输入要私信的用户主页ID" style="width: 400px"></el-input>
        <el-button @click="privateMessage" class="ml-2">私信</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import {random16} from "@/utils/string_util.js";
import {cookiesArray} from "../../electron/utils/cookies.js";
import modal from "../../electron/utils/modal.js";
import {ref} from "vue";


const followHomePageIdValue = ref("")
const privateMessageHomePageIdValue = ref("")
const webviewId = random16()

const test111 = () => {
  createNewWebview()
}

const goHomePage = async () => {
  await window.khj.puppeteer.goHomePage(JSON.stringify({webviewId: webviewId, cookiesArray: cookiesArray}))
}

const follow = async () => {
  const res = await window.khj.puppeteer.follow(JSON.stringify({webviewId: webviewId, homePageId: followHomePageIdValue.value, cookiesArray: cookiesArray}))
  if (res.code === 200) {
    modal.msgSuccess(res.data)
  } else {
    modal.msgWarning(res.data)
  }
}

const privateMessage = async () => {
  const res = await window.khj.puppeteer.privateMessage(JSON.stringify({webviewId: webviewId, homePageId: privateMessageHomePageIdValue.value, cookiesArray: cookiesArray}))
  if (res.code === 200) {
    modal.msgSuccess(res.data)
  } else {
    modal.msgWarning(res.data)
  }
}

// 创建新的
const createNewWebview = () => {
  const webview = document.createElement('webview')
  webview.setAttribute('id', webviewId)
  webview.setAttribute('src', 'http://www.baidu.com')
  webview.setAttribute('allowpopups', "true")
  webview.setAttribute('useragent', "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.215 Safari/537.36")
  webview.setAttribute('style', 'display:inline-flex;width:720px;height:500px;')
  // webview.setAttribute('partition', useAccountStore().curSelectedDouyinAccount.webviewPartition)
  document.getElementById('webviewDiv').appendChild(webview)
  webview.addEventListener("dom-ready", async() => {
    webview.openDevTools()
    await webview.executeJavaScript(`localStorage.setItem("webviewId","${webviewId}"  )`)
    console.log("dom-ready加载完成")
  })
}

</script>

<style scoped lang="scss">

</style>